package GUI;

import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import simulation.Data;
import simulation.Simulator;
import simulation.Tomasulo;


public class ProgramAndData extends JFrame implements ActionListener,ItemListener{

	private JPanel contentPane;
	private String[] immediateArray = new String[128];
	private String[] registersArray = new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"};
	private String[] registersArray2 = new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"};
	ArrayList<JComboBox> InstructionList = new ArrayList<JComboBox>();
	ArrayList<JComboBox> destinationsList = new ArrayList<JComboBox>();
	ArrayList<JComboBox> firstSrcsList = new ArrayList<JComboBox>();
	ArrayList<JComboBox> secondSrcsList = new ArrayList<JComboBox>();
	private JTextField textFieldR1;
	private JTextField textFieldR2;
	private JTextField textFieldR3;
	private JTextField textFieldR4;
	private JTextField textFieldR5;
	private JTextField textFieldR6;
	private JTextField textFieldR7;
	JButton btnTest;
	JButton btnAdd;
	JLabel lblError;
	DefaultTableModel tableModel = new DefaultTableModel();
	private JTextField textFieldAddress;
	private JTextField textFieldValue;
	private JTextField textFieldStartingAddress;
	ArrayList<Data> data = new ArrayList<Data>();
	boolean verified = true ;
	TextArea textArea;
	public int memoryCapacity;
	int numberOfInstructions;
	public int programStartingAddress;
	ArrayList<String> instructions = new ArrayList<String>();
	JButton btnCycleByCycle;
	JLabel lblWasNotAdded;
	HardwareOrganization hwo;

	/**

	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProgramAndData frame = new ProgramAndData("512",null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProgramAndData(String mem,HardwareOrganization ho) {
		setResizable(false);
		hwo = ho;
		memoryCapacity = Integer.parseInt(mem);
		setTitle("Program & Data");
		fillImArray();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(50, 50, 800, 619);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblProgram = new JLabel("Program");
		lblProgram.setBounds(10, 11, 68, 14);
		contentPane.add(lblProgram);
		
		btnTest = new JButton("Test");
		btnTest.setBounds(342, 547, 89, 35);
		btnTest.addActionListener(this);
		contentPane.add(btnTest);
		
		JComboBox comboBoxInst1 = new JComboBox();
		comboBoxInst1.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst1.setSelectedIndex(0);
		comboBoxInst1.setBounds(10, 36, 68, 20);
		comboBoxInst1.addItemListener(this);
		InstructionList.add(comboBoxInst1);
		contentPane.add(comboBoxInst1);
		
		JComboBox comboBoxDest1 = new JComboBox();
		comboBoxDest1.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxDest1.setBounds(81, 36, 45, 20);
		destinationsList.add(comboBoxDest1);
		contentPane.add(comboBoxDest1);
		
		JComboBox comboBoxFirstsrc1 = new JComboBox();
		comboBoxFirstsrc1.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxFirstsrc1.setBounds(148, 36, 46, 20);
		firstSrcsList.add(comboBoxFirstsrc1);
		contentPane.add(comboBoxFirstsrc1);
		
		JComboBox comboBoxInst2 = new JComboBox();
		comboBoxInst2.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst2.setBounds(10, 67, 68, 20);
		InstructionList.add(comboBoxInst2);
		comboBoxInst2.addItemListener(this);
		contentPane.add(comboBoxInst2);
		
		JComboBox comboBoxDest2 = new JComboBox();
		comboBoxDest2.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxDest2.setBounds(81, 67, 45, 20);
		destinationsList.add(comboBoxDest2);
		contentPane.add(comboBoxDest2);
		
		JComboBox comboBoxFirstsrc2 = new JComboBox();
		comboBoxFirstsrc2.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxFirstsrc2.setBounds(148, 67, 46, 20);
		firstSrcsList.add(comboBoxFirstsrc2);
		contentPane.add(comboBoxFirstsrc2);
		
		JComboBox comboBoxInst3 = new JComboBox();
		comboBoxInst3.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst3.setBounds(10, 98, 68, 20);
		InstructionList.add(comboBoxInst3);
		comboBoxInst3.addItemListener(this);
		contentPane.add(comboBoxInst3);
		
		JComboBox comboBoxDest3 = new JComboBox();
		comboBoxDest3.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxDest3.setBounds(81, 98, 45, 20);
		destinationsList.add(comboBoxDest3);
		contentPane.add(comboBoxDest3);
		
		JComboBox comboBoxDest4 = new JComboBox();
		comboBoxDest4.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxDest4.setBounds(81, 129, 45, 20);
		destinationsList.add(comboBoxDest4);
		contentPane.add(comboBoxDest4);
		
		JComboBox comboBoxFirstsrc3 = new JComboBox();
		comboBoxFirstsrc3.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxFirstsrc3.setBounds(148, 98, 46, 20);
		firstSrcsList.add(comboBoxFirstsrc3);
		contentPane.add(comboBoxFirstsrc3);

		JComboBox comboBoxFirstsrc4 = new JComboBox();
		comboBoxFirstsrc4.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxFirstsrc4.setBounds(148, 129, 46, 20);
		firstSrcsList.add(comboBoxFirstsrc4);
		contentPane.add(comboBoxFirstsrc4);
		
		JComboBox comboBox2ndsrc1 = new JComboBox();
		comboBox2ndsrc1.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBox2ndsrc1.setBounds(217, 36, 46, 20);
		secondSrcsList.add(comboBox2ndsrc1);
		contentPane.add(comboBox2ndsrc1);
		
		JComboBox comboBox2ndsrc2 = new JComboBox();
		comboBox2ndsrc2.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBox2ndsrc2.setBounds(217, 67, 46, 20);
		secondSrcsList.add(comboBox2ndsrc2);
		contentPane.add(comboBox2ndsrc2);
		
		JComboBox comboBox2ndsrc3 = new JComboBox();
		comboBox2ndsrc3.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBox2ndsrc3.setBounds(217, 98, 46, 20);
		secondSrcsList.add(comboBox2ndsrc3);
		contentPane.add(comboBox2ndsrc3);
		
		JComboBox comboBoxDest5 = new JComboBox();
		comboBoxDest5.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxDest5.setBounds(81, 160, 45, 20);
		destinationsList.add(comboBoxDest5);
		contentPane.add(comboBoxDest5);
		
		JComboBox comboBoxDest6 = new JComboBox();
		comboBoxDest6.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxDest6.setBounds(81, 191, 45, 20);
		destinationsList.add(comboBoxDest6);
		contentPane.add(comboBoxDest6);

		JComboBox comboBoxFirstsrc5 = new JComboBox();
		comboBoxFirstsrc5.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxFirstsrc5.setBounds(148, 160, 46, 20);
		firstSrcsList.add(comboBoxFirstsrc5);
		contentPane.add(comboBoxFirstsrc5);
		
		JComboBox comboBoxFirstsrc6 = new JComboBox();
		comboBoxFirstsrc6.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBoxFirstsrc6.setBounds(148, 191, 46, 20);
		firstSrcsList.add(comboBoxFirstsrc6);
		contentPane.add(comboBoxFirstsrc6);
		
		JComboBox comboBoxInst4 = new JComboBox();
		comboBoxInst4.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst4.setBounds(10, 129, 68, 20);
		InstructionList.add(comboBoxInst4);
		comboBoxInst4.addItemListener(this);
		contentPane.add(comboBoxInst4);
		
		
		JComboBox comboBox2ndsrc4 = new JComboBox();
		comboBox2ndsrc4.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBox2ndsrc4.setBounds(217, 129, 46, 20);
		secondSrcsList.add(comboBox2ndsrc4);
		contentPane.add(comboBox2ndsrc4);
		
		JComboBox comboBox2ndsrc5 = new JComboBox();
		comboBox2ndsrc5.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBox2ndsrc5.setBounds(217, 160, 46, 20);
		secondSrcsList.add(comboBox2ndsrc5);
		contentPane.add(comboBox2ndsrc5);
		
		JComboBox comboBox2ndsrc6 = new JComboBox();
		comboBox2ndsrc6.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		comboBox2ndsrc6.setBounds(217, 191, 46, 20);
		secondSrcsList.add(comboBox2ndsrc6);
		contentPane.add(comboBox2ndsrc6);
		
		
		JComboBox comboBoxInst5 = new JComboBox();
		comboBoxInst5.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst5.setBounds(10, 160, 68, 20);
		InstructionList.add(comboBoxInst5);
		comboBoxInst5.addItemListener(this);
		contentPane.add(comboBoxInst5);

		JComboBox comboBoxInst6 = new JComboBox();
		comboBoxInst6.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst6.setBounds(10, 191, 68, 20);
		InstructionList.add(comboBoxInst6);
		comboBoxInst6.addItemListener(this);
		contentPane.add(comboBoxInst6);
		
		JComboBox comboBoxInst7 = new JComboBox();
		comboBoxInst7.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst7.setBounds(10, 222, 68, 20);
		InstructionList.add(comboBoxInst7);
		comboBoxInst7.addItemListener(this);
		contentPane.add(comboBoxInst7);
		
		
		JComboBox comboBoxInst8 = new JComboBox();
		comboBoxInst8.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst8.setBounds(10, 253, 68, 20);
		InstructionList.add(comboBoxInst8);
		comboBoxInst8.addItemListener(this);
		contentPane.add(comboBoxInst8);
		
		JComboBox comboBoxInst9 = new JComboBox();
		comboBoxInst9.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst9.setBounds(10, 284, 68, 20);
		InstructionList.add(comboBoxInst9);
		comboBoxInst9.addItemListener(this);
		contentPane.add(comboBoxInst9);
		
		
		JComboBox comboBoxInst10 = new JComboBox();
		comboBoxInst10.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst10.setBounds(10, 315, 68, 20);
		InstructionList.add(comboBoxInst10);
		comboBoxInst10.addItemListener(this);
		contentPane.add(comboBoxInst10);
		
		JComboBox comboBoxInst11 = new JComboBox();
		comboBoxInst11.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst11.setBounds(10, 346, 68, 20);
		InstructionList.add(comboBoxInst11);
		comboBoxInst11.addItemListener(this);
		contentPane.add(comboBoxInst11);
		
		JComboBox comboBoxInst12 = new JComboBox();
		comboBoxInst12.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst12.setBounds(10, 377, 68, 20);
		comboBoxInst12.addItemListener(this);
		InstructionList.add(comboBoxInst12);
		contentPane.add(comboBoxInst12);
		
		JComboBox comboBoxInst13 = new JComboBox();
		comboBoxInst13.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst13.setBounds(10, 408, 68, 20);
		comboBoxInst13.addItemListener(this);
		InstructionList.add(comboBoxInst13);
		contentPane.add(comboBoxInst13);
		
		JComboBox comboBoxInst14 = new JComboBox();
		comboBoxInst14.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst14.setBounds(10, 439, 68, 20);
		comboBoxInst14.addItemListener(this);
		InstructionList.add(comboBoxInst14);
		contentPane.add(comboBoxInst14);
		
		JComboBox comboBoxInst15 = new JComboBox();
		comboBoxInst15.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst15.setBounds(10, 470, 68, 20);
		comboBoxInst15.addItemListener(this);
		InstructionList.add(comboBoxInst15);
		contentPane.add(comboBoxInst15);
		
		JComboBox comboBoxInst16 = new JComboBox();
		comboBoxInst16.setModel(new DefaultComboBoxModel(new String[] {"", "LW", "SW", "JMP", "BEQ", "JALR", "RET", "ADD", "ADDI", "NAND", "MUL", "DIV"}));
		comboBoxInst16.setBounds(10, 501, 68, 20);
		comboBoxInst16.addItemListener(this);
		InstructionList.add(comboBoxInst16);
		contentPane.add(comboBoxInst16);

		
		JComboBox comboBoxFirstsrc7 = new JComboBox();
		comboBoxFirstsrc7.setBounds(148, 222, 46, 20);
		comboBoxFirstsrc7.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc7);
		contentPane.add(comboBoxFirstsrc7);
		
		JComboBox comboBox2ndsrc7 = new JComboBox();
		comboBox2ndsrc7.setBounds(217, 222, 46, 20);
		comboBox2ndsrc7.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc7);
		contentPane.add(comboBox2ndsrc7);
		
		JComboBox comboBox2ndsrc8 = new JComboBox();
		comboBox2ndsrc8.setBounds(217, 253, 46, 20);
		comboBox2ndsrc8.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc8);
		contentPane.add(comboBox2ndsrc8);
		
		JComboBox comboBoxFirstsrc8 = new JComboBox();
		comboBoxFirstsrc8.setBounds(148, 253, 46, 20);
		comboBoxFirstsrc8.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc8);
		contentPane.add(comboBoxFirstsrc8);
		
		JComboBox comboBoxDest7 = new JComboBox();
		comboBoxDest7.setBounds(81, 222, 45, 20);
		comboBoxDest7.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest7);
		contentPane.add(comboBoxDest7);
		
		JComboBox comboBoxDest8 = new JComboBox();
		comboBoxDest8.setBounds(81, 253, 45, 20);
		comboBoxDest8.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest8);
		contentPane.add(comboBoxDest8);
		
		JComboBox comboBoxDest9 = new JComboBox();
		comboBoxDest9.setBounds(81, 284, 45, 20);
		comboBoxDest9.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest9);
		contentPane.add(comboBoxDest9);
		
		JComboBox comboBoxFirstsrc9 = new JComboBox();
		comboBoxFirstsrc9.setBounds(148, 284, 46, 20);
		comboBoxFirstsrc9.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc9);
		contentPane.add(comboBoxFirstsrc9);
		
		JComboBox comboBox2ndsrc9 = new JComboBox();
		comboBox2ndsrc9.setBounds(217, 284, 46, 20);
		comboBox2ndsrc9.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc9);
		contentPane.add(comboBox2ndsrc9);
		
		JComboBox comboBox2ndsrc10 = new JComboBox();
		comboBox2ndsrc10.setBounds(217, 315, 46, 20);
		comboBox2ndsrc10.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc10);
		contentPane.add(comboBox2ndsrc10);
		
		
		JComboBox comboBoxDest10 = new JComboBox();
		comboBoxDest10.setBounds(81, 315, 45, 20);
		comboBoxDest10.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest10);
		contentPane.add(comboBoxDest10);
		
		
		JComboBox comboBoxDest11 = new JComboBox();
		comboBoxDest11.setBounds(81, 346, 45, 20);
		comboBoxDest11.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest11);
		contentPane.add(comboBoxDest11);
		
		JComboBox comboBox2ndsrc11 = new JComboBox();
		comboBox2ndsrc11.setBounds(217, 346, 46, 20);
		comboBox2ndsrc11.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc11);
		contentPane.add(comboBox2ndsrc11);
		
		
		JComboBox comboBoxDest12 = new JComboBox();
		comboBoxDest12.setBounds(81, 377, 45, 20);
		comboBoxDest12.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest12);
		contentPane.add(comboBoxDest12);
		
		JComboBox comboBoxDest13 = new JComboBox();
		comboBoxDest13.setBounds(81, 408, 45, 20);
		comboBoxDest13.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest13);
		contentPane.add(comboBoxDest13);
		
		JComboBox comboBoxDest14 = new JComboBox();
		comboBoxDest14.setBounds(81, 439, 45, 20);
		comboBoxDest14.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest14);
		contentPane.add(comboBoxDest14);
		
		JComboBox comboBoxDest15 = new JComboBox();
		comboBoxDest15.setBounds(81, 470, 45, 20);
		comboBoxDest15.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest15);
		contentPane.add(comboBoxDest15);
		
		JComboBox comboBoxDest16 = new JComboBox();
		comboBoxDest16.setBounds(81, 501, 45, 20);
		comboBoxDest16.setModel(new DefaultComboBoxModel(new String[] {"R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		destinationsList.add(comboBoxDest16);
		contentPane.add(comboBoxDest16);
		
		JComboBox comboBox2ndsrc12 = new JComboBox();
		comboBox2ndsrc12.setBounds(217, 377, 46, 20);
		comboBox2ndsrc12.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc12);
		contentPane.add(comboBox2ndsrc12);
		
		JComboBox comboBox2ndsrc13 = new JComboBox();
		comboBox2ndsrc13.setBounds(217, 408, 46, 20);
		comboBox2ndsrc13.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc13);
		contentPane.add(comboBox2ndsrc13);
		
		JComboBox comboBox2ndsrc14 = new JComboBox();
		comboBox2ndsrc14.setBounds(217, 439, 46, 20);
		comboBox2ndsrc14.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc14);
		contentPane.add(comboBox2ndsrc14);
		
		JComboBox comboBox2ndsrc15 = new JComboBox();
		comboBox2ndsrc15.setBounds(217, 470, 46, 20);
		comboBox2ndsrc15.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc15);
		contentPane.add(comboBox2ndsrc15);
		
		JComboBox comboBox2ndsrc16 = new JComboBox();
		comboBox2ndsrc16.setBounds(217, 501, 46, 20);
		comboBox2ndsrc16.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		secondSrcsList.add(comboBox2ndsrc16);
		contentPane.add(comboBox2ndsrc16);
		
		JComboBox comboBoxFirstsrc10 = new JComboBox();
		comboBoxFirstsrc10.setBounds(148, 315, 46, 20);
		comboBoxFirstsrc10.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc10);
		contentPane.add(comboBoxFirstsrc10);
		
		JComboBox comboBoxFirstsrc11 = new JComboBox();
		comboBoxFirstsrc11.setBounds(148, 346, 46, 20);
		comboBoxFirstsrc11.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc11);
		contentPane.add(comboBoxFirstsrc11);
		
		JComboBox comboBoxFirstsrc12 = new JComboBox();
		comboBoxFirstsrc12.setBounds(148, 377, 46, 20);
		comboBoxFirstsrc12.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc12);
		contentPane.add(comboBoxFirstsrc12);
		
		JComboBox comboBoxFirstsrc13 = new JComboBox();
		comboBoxFirstsrc13.setBounds(148, 408, 46, 20);
		comboBoxFirstsrc13.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc13);
		contentPane.add(comboBoxFirstsrc13);
		
		JComboBox comboBoxFirstsrc14 = new JComboBox();
		comboBoxFirstsrc14.setBounds(148, 439, 46, 20);
		comboBoxFirstsrc14.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc14);
		contentPane.add(comboBoxFirstsrc14);
		
		JComboBox comboBoxFirstsrc15 = new JComboBox();
		comboBoxFirstsrc15.setBounds(148, 470, 46, 20);
		comboBoxFirstsrc15.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc15);
		contentPane.add(comboBoxFirstsrc15);
		
		JComboBox comboBoxFirstsrc16 = new JComboBox();
		comboBoxFirstsrc16.setBounds(148, 501, 46, 20);
		comboBoxFirstsrc16.setModel(new DefaultComboBoxModel(new String[] {"R0","R1", "R2", "R3", "R4", "R5", "R6", "R7"}));
		firstSrcsList.add(comboBoxFirstsrc16);
		contentPane.add(comboBoxFirstsrc16);
		
		
		JLabel lblData = new JLabel("Data");
		lblData.setBounds(363, 327, 46, 14);
		contentPane.add(lblData);
		
		JLabel lblR = new JLabel("R1");
		lblR.setBounds(312, 39, 46, 14);
		contentPane.add(lblR);
		
		JLabel lblR_3 = new JLabel("R2");
		lblR_3.setBounds(312, 70, 46, 14);
		contentPane.add(lblR_3);
		
		JLabel lblR_1 = new JLabel("R3");
		lblR_1.setBounds(312, 101, 46, 14);
		contentPane.add(lblR_1);
		
		JLabel lblR_2 = new JLabel("R4");
		lblR_2.setBounds(312, 132, 46, 14);
		contentPane.add(lblR_2);
		
		JLabel lblR_4 = new JLabel("R5");
		lblR_4.setBounds(312, 163, 46, 14);
		contentPane.add(lblR_4);
		
		JLabel lblR_6 = new JLabel("R6");
		lblR_6.setBounds(312, 194, 46, 14);
		contentPane.add(lblR_6);
		
		JLabel lblR_5 = new JLabel("R7");
		lblR_5.setBounds(312, 225, 46, 14);
		contentPane.add(lblR_5);
		
		textFieldR1 = new JTextField();
		textFieldR1.setText("0");
		textFieldR1.setBounds(343, 36, 68, 20);
		contentPane.add(textFieldR1);
		textFieldR1.setColumns(10);
		
		textFieldR2 = new JTextField();
		textFieldR2.setText("0");
		textFieldR2.setColumns(10);
		textFieldR2.setBounds(343, 67, 68, 20);
		contentPane.add(textFieldR2);
		
		textFieldR3 = new JTextField();
		textFieldR3.setText("0");
		textFieldR3.setColumns(10);
		textFieldR3.setBounds(343, 98, 68, 20);
		contentPane.add(textFieldR3);
		
		textFieldR4 = new JTextField();
		textFieldR4.setText("0");
		textFieldR4.setColumns(10);
		textFieldR4.setBounds(343, 129, 68, 20);
		contentPane.add(textFieldR4);
		
		textFieldR5 = new JTextField();
		textFieldR5.setText("0");
		textFieldR5.setColumns(10);
		textFieldR5.setBounds(343, 160, 68, 20);
		contentPane.add(textFieldR5);
		
		textFieldR6 = new JTextField();
		textFieldR6.setText("0");
		textFieldR6.setColumns(10);
		textFieldR6.setBounds(343, 191, 68, 20);
		contentPane.add(textFieldR6);
		
		textFieldR7 = new JTextField();
		textFieldR7.setText("0");
		textFieldR7.setColumns(10);
		textFieldR7.setBounds(343, 222, 68, 20);
		contentPane.add(textFieldR7);
		
		lblError = new JLabel("Please choose applicable values");
		lblError.setVisible(false);
		lblError.setForeground(Color.RED);
		lblError.setBounds(312, 513, 279, 23);
		contentPane.add(lblError);
		
		JLabel lblRegisters = new JLabel("Registers");
		lblRegisters.setBounds(342, 11, 107, 14);
		contentPane.add(lblRegisters);
		tableModel.addColumn("Address");
		tableModel.addColumn("Value");
		
		btnAdd = new JButton("Add");
		btnAdd.setBounds(419, 419, 68, 23);
		btnAdd.addActionListener(this);
		contentPane.add(btnAdd);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(298, 349, 79, 14);
		contentPane.add(lblAddress);
		
		JLabel lblValue = new JLabel("Value");
		lblValue.setBounds(401, 352, 48, 14);
		contentPane.add(lblValue);
		
		textFieldAddress = new JTextField();
		textFieldAddress.setBounds(298, 377, 86, 20);
		contentPane.add(textFieldAddress);
		textFieldAddress.setColumns(10);
		
		textFieldValue = new JTextField();
		textFieldValue.setColumns(10);
		textFieldValue.setBounds(401, 377, 86, 20);
		contentPane.add(textFieldValue);
		
		JLabel lblStartingAddress = new JLabel("Starting Address");
		lblStartingAddress.setBounds(298, 284, 111, 20);
		contentPane.add(lblStartingAddress);
		
		textFieldStartingAddress = new JTextField();
		textFieldStartingAddress.setColumns(10);
		textFieldStartingAddress.setBounds(401, 284, 86, 20);
		contentPane.add(textFieldStartingAddress);
		
		textArea = new TextArea();
		textArea.setEditable(false);
		textArea.setBounds(538, 64, 220, 405);
		contentPane.add(textArea);
		
		JLabel lblMemory = new JLabel("Memory");
		lblMemory.setBounds(617, 33, 120, 27);
		contentPane.add(lblMemory);
		
		btnCycleByCycle = new JButton("Cycle by cycle test");
		btnCycleByCycle.setBounds(500, 547, 175, 35);
		btnCycleByCycle.addActionListener(this);
		contentPane.add(btnCycleByCycle);
		
		lblWasNotAdded = new JLabel("Must be numbers !!");
		lblWasNotAdded.setVisible(false);
		lblWasNotAdded.setForeground(Color.RED);
		lblWasNotAdded.setBounds(305, 440, 220, 29);
		contentPane.add(lblWasNotAdded);
		
		this.setVisible(true);
		
	}

	private void fillImArray() {
		for (int i = 0,value=-63; i < immediateArray.length; i++,value++) {
			immediateArray[i]=value + "";
		}
		
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
//		LW
//		SW
//		JMP
//		BEQ
//		JALR
//		RET
//		ADD
//		ADDI
//		NAND
//		MUL
//		DIV
		if(InstructionList.contains(e.getSource()))
		{
			int tempInstr =((JComboBox)e.getSource()).getSelectedIndex();
			switch (tempInstr) {
			//load
			case 1:
			case 8:
				int pos = InstructionList.indexOf(e.getSource());
				firstSrcsList.get(pos).setEnabled(true);
				secondSrcsList.get(pos).setEnabled(true);
				destinationsList.get(pos).setModel(new DefaultComboBoxModel(registersArray2));
				firstSrcsList.get(pos).setModel(new DefaultComboBoxModel(registersArray));
				secondSrcsList.get(pos).setModel(new DefaultComboBoxModel(immediateArray));
				break;
			//store
			//branch
			case 2:
			case 4:
				pos = InstructionList.indexOf(e.getSource());
				firstSrcsList.get(pos).setEnabled(true);
				secondSrcsList.get(pos).setEnabled(true);
				destinationsList.get(pos).setModel(new DefaultComboBoxModel(registersArray));
				firstSrcsList.get(pos).setModel(new DefaultComboBoxModel(registersArray));
				secondSrcsList.get(pos).setModel(new DefaultComboBoxModel(immediateArray));
				break;
			//jump
			case 3:
				pos = InstructionList.indexOf(e.getSource());
				secondSrcsList.get(pos).setEnabled(false);
				destinationsList.get(pos).setModel(new DefaultComboBoxModel(registersArray));
				firstSrcsList.get(pos).setModel(new DefaultComboBoxModel(immediateArray));
				break;
			
			//Jump and link
			case 5 :
				pos = InstructionList.indexOf(e.getSource());
				secondSrcsList.get(pos).setEnabled(false);
				destinationsList.get(pos).setModel(new DefaultComboBoxModel(registersArray2));
				firstSrcsList.get(pos).setModel(new DefaultComboBoxModel(registersArray));
				
				break;
			//Return
			case 6 :
				pos = InstructionList.indexOf(e.getSource());
				destinationsList.get(pos).setModel(new DefaultComboBoxModel(registersArray));
				secondSrcsList.get(pos).setEnabled(false);
				firstSrcsList.get(pos).setEnabled(false);
				
				break;
			default:
				pos = InstructionList.indexOf(e.getSource());
				destinationsList.get(pos).setModel(new DefaultComboBoxModel(registersArray2));
				secondSrcsList.get(pos).setEnabled(true);
				firstSrcsList.get(pos).setEnabled(true);
				firstSrcsList.get(pos).setModel(new DefaultComboBoxModel(registersArray));
				secondSrcsList.get(pos).setModel(new DefaultComboBoxModel(registersArray));
				break;
			}
		}
			
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnTest||e.getSource()==btnCycleByCycle)
		{
			try
			{
				int r1 = Integer.parseInt(textFieldR1.getText());
				int r2 = Integer.parseInt(textFieldR2.getText());
				int r3 = Integer.parseInt(textFieldR3.getText());
				int r4 = Integer.parseInt(textFieldR4.getText());
				int r5 = Integer.parseInt(textFieldR5.getText());
				int r6 = Integer.parseInt(textFieldR6.getText());
				int r7 = Integer.parseInt(textFieldR7.getText());
				Integer.parseInt(textFieldStartingAddress.getText());
				
				hwo.mh.simulator.registerFile[1]=r1;
				hwo.mh.simulator.registerFile[2]=r2;
				hwo.mh.simulator.registerFile[3]=r3;
				hwo.mh.simulator.registerFile[4]=r4;
				hwo.mh.simulator.registerFile[5]=r5;
				hwo.mh.simulator.registerFile[6]=r6;
				hwo.mh.simulator.registerFile[7]=r7;
					
				// assigning the values
				programStartingAddress = Integer.parseInt(textFieldStartingAddress.getText());
				//System.out.println(InstructionList.size());
				for (int i = 0; i < InstructionList.size(); i++) {
					if(InstructionList.get(i).getSelectedItem().equals(""))
					{
						numberOfInstructions = i;
						break;
					}
				}
				if(numberOfInstructions==0)
				{
					lblError.setVisible(true);
					return;
				}
				//Simulator.initializeArray(numberOfInstructions);
				hwo.mh.simulator.programStartingAddress = programStartingAddress;
				System.out.println(hwo.mh.simulator.programStartingAddress);
				if(data.size()>0)
				{
					Data[] theData = new Data[data.size()];
					for (int i = 0; i < theData.length; i++) {
						theData[i]=data.get(i);
					}
					hwo.mh.simulator.data = theData;
				}
				
				
				for (int i = 0; i < numberOfInstructions; i++) {
					if(secondSrcsList.get(i).isEnabled())
					{
						instructions.add(((String)(InstructionList.get(i).getSelectedItem()))+" "+(((String)(destinationsList.get(i).getSelectedItem())))+","+(((String)(firstSrcsList.get(i).getSelectedItem()))+","+((String)(secondSrcsList.get(i).getSelectedItem()))));
					}
					else if(firstSrcsList.get(i).isEnabled())
					{
						instructions.add(((String)(InstructionList.get(i).getSelectedItem()))+" "+(((String)(destinationsList.get(i).getSelectedItem())))+","+(((String)(firstSrcsList.get(i).getSelectedItem()))));
					}
					else
					{
						instructions.add(((String)(InstructionList.get(i).getSelectedItem()))+" "+(((String)(destinationsList.get(i).getSelectedItem()))));
					}
						
					
				}
				System.out.println(numberOfInstructions);
				String[] pro = new String[numberOfInstructions];
				for (int i = 0; i < pro.length; i++) {
					pro[i]=instructions.get(i);
				}
				hwo.mh.simulator.program = pro;
				System.out.println("pnd"+hwo.mh.tomasulo.simulator.program[0]);
				
				for (int j=0 ,i = hwo.mh.tomasulo.simulator.programStartingAddress; j < pro.length; j++,i++) {
					hwo.mh.tomasulo.simulator.memory.put(i, pro[j]);
				}
				for (int i = 0; i < data.size(); i++) {
					hwo.mh.tomasulo.simulator.memory.put(data.get(i).getAddress(), data.get(i).getValue());
				}
				
				hwo.mh.tomasulo = new Tomasulo(hwo.mh.simulator);
				
				//System.out.println("pnd"+hwo.mh.tomasulo.simulator.program[1]);
				this.dispose();
				if(e.getSource()==btnTest)
				{
					Output out = new Output(this);
				}
				else
				{
					Cycle c = new Cycle(0,12,this);
				}
				
			}
			catch (NumberFormatException n) {
				lblError.setVisible(true);
			}
		}
		else if(e.getSource()==btnAdd)
		{
//			tableModel.insertRow(tableModel.getRowCount(), new Object[]{0,0});
//			tableModel.addRow(new Object [] {0,0});
//			tableData = new JTable(tableModel);
//			tableData.validate();
//			tableData.repaint();
//			tableData = new JTable(tableModel);
//			tableData.setValueAt(aValue, row, column)
			int q = 0;
			try
			{
				
				String memory="";
				int dataAddress = Integer.parseInt(textFieldAddress.getText());
				int dataValue = Integer.parseInt(textFieldValue.getText());
				lblWasNotAdded.setVisible(false);
				textFieldAddress.setText("");
				textFieldValue.setText("");
				
				q=1;
				int startingAddress = Integer.parseInt(textFieldStartingAddress.getText());
				for (int i = 0; i < InstructionList.size(); i++) {
					if(InstructionList.get(i).getSelectedItem().equals(""))
					{
						numberOfInstructions = i;
						break;
					}
				}
				if(numberOfInstructions==0)
				{
					//lblError.setVisible(true);
					return;
				}
				if(dataAddress<memoryCapacity/2&&dataAddress>=0&&(dataAddress<startingAddress||dataAddress>=startingAddress+numberOfInstructions))
				{
					if(data.contains(new Data(0,dataAddress)))
					{
						data.set(data.indexOf(new Data(0,dataAddress)), new Data(dataValue,dataAddress));
					}
					else
					{
						data.add(new Data(dataValue,dataAddress));
					}
					
					for (int i = 0; i < data.size(); i++) {
						memory+=data.get(i)+"\n";
					}
					textArea.setText(memory);
					this.validate();
				}
				
			}
			catch (NumberFormatException n) {
				if(q==0)
				{
					lblWasNotAdded.setText("Must be Numbers !!");
					lblWasNotAdded.setVisible(true);
				}
				else
				{
					lblWasNotAdded.setText("Enter the starting address");
					lblWasNotAdded.setVisible(true);
				}
				
				
			}
		}
		
	}
}
